(function(window){

    function Progress($progressBar, $progressLine, $progressDot){
        return new Progress.prototype.init($progressBar, $progressLine, $progressDot);
    }
    Progress.prototype = {
        constructor:Progress,
        isMove:false,
        init:function($progressBar, $progressLine, $progressDot){
        	this.$progressBar = $progressBar;
            this.$progressLine =  $progressLine;
            this.$progressDot = $progressDot;
        },
        progressClick:function(callBack){
            var $this = this;
            // 监听背景的点击
            var normalLeft = $this.$progressBar.offset().left;
            var barWidth = $this.$progressBar.width();
            this.$progressBar.click(function(event){
                //获取背景距离窗口默认的位置
                // console.log(normalLeft)
                var eventLeft = event.pageX;
                // console.log(event)
                var offset = eventLeft-normalLeft;
                //设置前景宽度
                if(offset >=0 && offset <=barWidth){
                    $this.$progressLine.css("width",offset)
                    $this.$progressDot.css("left",offset) 
                    // console.log(offset)
                    //计算进度条的比例
                    var value = (eventLeft-normalLeft) / $this.$progressBar.width();
                    callBack(value);
                }  
            });

        },
        progressMove:function(callBack){
            var $this = this;
            var eventLeft;
            var normalLeft = this.$progressBar.offset().left;
            var barWidth = this.$progressBar.width();
            //监听鼠标的按下事件
            this.$progressBar.mousedown(function(){
            //获取背景距离窗口默认的位置
            $this.isMove=true;
            //监听鼠标的移动事件
               $(document).mousemove(function(){
                    //获取背景距离窗口默认的位置
                          
                    eventLeft = event.pageX;
                    var offset = eventLeft-normalLeft;
                    //设置前景宽度
                    // console.log(offset)

                    if(offset >=0 && offset <=barWidth){
                        $this.$progressLine.css("width",offset)
                        $this.$progressDot.css("left",offset)
                }

                 });
            });  
             $(document).mouseup(function(){

                $(document).off("mousemove")   //监听鼠标的抬起事件
                $this.isMove = false;
                //计算进度条的比例
                var value = (eventLeft-normalLeft) / $this.$progressBar.width();
                callBack(value);
            });       
                
        },
        setProgress:function(value){
            if(this.isMove) return;
            if(value<0 || value>100) return;
            this.$progressLine.css({
                width:value+"%"
            });
            this.$progressDot.css({
                left:value+"%"
            });
        }

    }
    Progress.prototype.init.prototype = Progress.prototype;
    window.Progress = Progress;

})(window);